package DataBase;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class ResetManager {
	
	
	
	
	
	
	//动态显示
	public static ArrayList<Reset> select() 
	{
		DbcpBean dbcp = new DbcpBean();
		Connection conn = dbcp.getConnection();
		ResultSet result = null;
		ArrayList<Reset> dss = new ArrayList<Reset>();
		if(conn != null)
		{
			try 
			{
				Statement stmt = conn.createStatement();
				String sql = null;
				
			
				
				sql = "select tb_datasource.ds_ID,fu_ID, ds_hostname,ds_IP,asmt_state,asmt_name,asmt_path,asmt_next,asmt_time,asmt_frequency" +
							" from tb_datasource,tb_assignment " +
							"where tb_datasource.ds_ID = tb_assignment.ds_ID";



				result  = stmt.executeQuery(sql);
				while(result.next()){
					Reset ds = new Reset();
					ds.setDs_ID(result.getInt("ds_ID"));
					ds.setDs_hostname(result.getString("ds_hostname"));
					ds.setDs_IP(result.getString("ds_IP"));
					ds.setFu_ID(result.getInt("fu_ID"));
					ds.setAsmt_names(result.getString("asmt_name"));
					ds.setAsmt_states(result.getString("asmt_state"));
					ds.setAsmt_path(result.getString("asmt_path"));
					ds.setAsmt_next(result.getString("asmt_next"));
					ds.setAsmt_time(result.getNString("asmt_time"));
					ds.setAsmt_frequency(result.getString("asmt_frequency"));
					dss.add(ds);
				}
//				for(Reset rs:dss){
//					System.out.println(rs.getAsmt_name());
//				}
			} 
			catch (SQLException e) 
			{
				try {
					conn.close();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				e.printStackTrace();
			}			
		}
		try 
		{
			conn.close();
		} 
		catch (SQLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dss;
	}
	
	//select by  ds_ID
	
	
	public static ArrayList<Reset> select(int ds_ID) 
	{
		DbcpBean dbcp = new DbcpBean();
		Connection conn = dbcp.getConnection();
		ResultSet result = null;
		ArrayList<Reset> dss = new  ArrayList<Reset>();
		if(conn != null)
		{
			try 
			{
				Statement stmt = conn.createStatement();
				String sql = null;
				
		
				sql = "select  tb_datasource.ds_ID,fu_ID,ds_hostname,ds_IP,asmt_state,asmt_name,asmt_path,asmt_next,asmt_time,asmt_frequency,asmt_fptname,asmt_fptpwd" +
							" from tb_datasource,tb_assignment " +
							"where (tb_datasource.ds_ID ="+ds_ID+" or tb_assignment.ds_ID ="+ds_ID +") and tb_datasource.ds_ID = tb_assignment.ds_ID;" ;
				
//				sql = "select asmt_state,asmt_name,asmt_path,asmt_next,asmt_time,asmt_frequency" +
//				" tb_assignment " +
//				"where tb_assignment.ds_ID ="+ds_ID+"";

				
				
				result  = stmt.executeQuery(sql);
				
				    if(result.next()){
					Reset ds = new Reset();
					ds.setDs_ID(result.getInt("ds_ID"));
//					System.out.println("wokao");
					ds.setDs_hostname(result.getString("ds_hostname"));
					ds.setDs_IP(result.getString("ds_IP"));
					ds.setFu_ID(result.getInt("fu_ID"));
					ds.setAsmt_names(result.getString("asmt_name"));
					ds.setAsmt_states(result.getString("asmt_state"));
					ds.setAsmt_path(result.getString("asmt_path"));
					ds.setAsmt_next(result.getString("asmt_next"));
					ds.setAsmt_time(result.getNString("asmt_time"));
					ds.setAsmt_frequency(result.getString("asmt_frequency"));
					dss.add(ds);
				}
//				for(Reset rs:dss){
//					System.out.println(rs.getAsmt_name());
//				}
			} 
			catch (SQLException e) 
			{
				try {
					conn.close();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				e.printStackTrace();
			}			
		}
		try 
		{
			conn.close();
		} 
		catch (SQLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return dss;
	}
	
	
	
	
	
	
	
	
	
	//删除操作
	public static boolean delete(int ds_ID) 
	{
		DbcpBean dbcp = new DbcpBean();
		Connection conn = dbcp.getConnection();
	//	ResultSet result = null;
		int result1=0;
		int result2=0;
//		ArrayList<Reset> dss = new ArrayList<Reset>();
		if(conn != null)
		{
			try 
			{
				Statement stmt = conn.createStatement();
				String sql = null;
				
			
				
				//sql = "delete from tb_datasource,tb_assignment where tb_datasource.ds_ID ="+ds_ID+",tb_assignment.ds_ID ="+ds_ID+"";
				sql = "delete from tb_datasource where ds_id = " + ds_ID;
				result1 = stmt.executeUpdate(sql);
				
				sql = "delete from tb_assignment where ds_id = " + ds_ID;
				result2 = stmt.executeUpdate(sql);
				
				
//				while(result.next()){
//					Reset ds = new Reset();
//					ds.setDs_hostname(result.getString("ds_hostname"));
//					ds.setDs_IP(result.getString("ds_IP"));
//					ds.setAsmt_names(result.getString("asmt_name"));
//					ds.setAsmt_path(result.getString("asmt_path"));
//					ds.setAsmt_next(result.getString("asmt_next"));
//					ds.setAsmt_time(result.getNString("asmt_time"));
//					ds.setAsmt_frequency(result.getString("asmt_frequency"));
//					dss.add(ds);
				}
//				for(Reset rs:dss){
//					System.out.println(rs.getAsmt_name());
//				}
			
			catch (SQLException e) 
			{
				try {
					conn.close();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				e.printStackTrace();
			}			
		}
		try 
		{
			conn.close();
		} 
		catch (SQLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (result1>0 && result2>0);
	}
		
	
	
	
	//修改操作
	public static boolean update(Reset rs) 
	{
		DbcpBean dbcp = new DbcpBean();
		Connection conn = dbcp.getConnection();
		int result = 0;
//		ResultSet result = null;
//		ArrayList<Reset> dss = new  ArrayList<Reset>();
		if(conn != null)
		{
			try 
			{
				Statement stmt = conn.createStatement();
				String sql = null;
				String asName = rs.getAsmt_name();
				int asfu_ID = rs.getFu_ID();
				String aspath = rs.getAsmt_path();
				String asnext = rs.getAsmt_next();
				String astime = rs.getAsmt_time();
				String asfrequency = rs.getAsmt_frequency();
				int asdsID = rs.getDs_ID();
//				System.out.println(aspath);
		
//				sql = "select  ds_hostname,ds_IP,asmt_state,asmt_name,asmt_path,asmt_next,asmt_time,asmt_frequency" +
//							" from tb_datasource,tb_assignment " +
//							"where (tb_datasource.ds_ID ="+ds_ID+" or tb_assignment.ds_ID ="+ds_ID +") and tb_datasource.ds_ID = tb_assignment.ds_ID;" ;
				sql = "update tb_assignment set asmt_name = '"+asName+",fu_ID = '" + asfu_ID + "',asmt_path = '"+aspath+"',asmt_next = '"+asnext+"',asmt_time = '"+astime+"',asmt_frequency = '"+asfrequency+"'"
				      +"where tb_assignment.ds_ID = "+asdsID+";";


				
				
				 result = stmt.executeUpdate(sql);
//				System.out.println(result);
//				    if(result.next()){
//					Reset ds = new Reset();
//  				ds.setDs_ID(result.getInt("ds_ID"));
//					ds.setDs_hostname(result.getString("ds_hostname"));
//					ds.setDs_IP(result.getString("ds_IP"));
//					ds.setAsmt_names(result.getString("asmt_name"));
//					ds.setAsmt_states(result.getString("asmt_state"));
//					ds.setAsmt_path(result.getString("asmt_path"));
//					ds.setAsmt_next(result.getString("asmt_next"));
//					ds.setAsmt_time(result.getNString("asmt_time"));
//					ds.setAsmt_frequency(result.getString("asmt_frequency"));
//					dss.add(ds);
				}
//				for(Reset rs:dss){
//					System.out.println(rs.getAsmt_name());
//				}
//			} 
			catch (SQLException e) 
			{
				try {
					conn.close();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				e.printStackTrace();
			}			
		}
		try 
		{
			conn.close();
		} 
		catch (SQLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
				return result>0;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
//	public static void main(String args[])
//	{
//		ResetManager.select();
//		//rsd.select();
//	}
//	
}
